﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;


public class UIBattleRoot : ViewUI
{

    public override void Update()
    {
        base.Update();
    }

    public override bool Init()
    {
        base.Init();

        this._ui_root = PrefabsMgr.Load(DATA.UI_PREFABS_FILE_BATTLE);


        EventDispatcher.ins.PostEvent("addAsync", new Func<int>(() =>
        {
            this._ui_child.Add(ViewUI.Create<UI_fpsms>(this));
            return 10;
        }));


        EventDispatcher.ins.PostEvent("addAsync", new Func<int>(() =>
        {
            this._ui_child.Add(ViewUI.Create<UI_rank>(this));
            return 40;
        }));


        EventDispatcher.ins.PostEvent("addAsync", new Func<int>(() =>
        {
            this._ui_child.Add(ViewUI.Create<UI_frame>(this));

            return 50;
        }));
        EventDispatcher.ins.PostEvent("addAsync", new Func<int>(() =>
        {
            this._ui_child.Add(ViewUI.Create<UI_video>(this));

            return 70;
        }));
        EventDispatcher.ins.PostEvent("addAsync", new Func<int>(() =>
        {
            this._ui_child.Add(ViewUI.Create<UI_death>(this));
            return 80;
        }));
        EventDispatcher.ins.PostEvent("addAsync", new Func<int>(() =>
        {
            this._ui_child.Add(ViewUI.Create<UI_xy>(this));

            this._ui_child.Add(ViewUI.Create<UI_btn_chat>(this));
            this._ui_child.Add(ViewUI.Create<UI_btn_child>(this));
            this._ui_child.Add(ViewUI.Create<UI_btn_split>(this));



            return 90;
        }));
        EventDispatcher.ins.PostEvent("addAsync", new Func<int>(() =>
        {
            this._ui_child.Add(ViewUI.Create<UI_info>(this));
            if (PublicData.GetInstance().isVideoMode == false)
            {
                this._ui_child.Add(ViewUI.Create<UI_dirInput>(this));
            }
            return 100;
        }));


        return true;
    }

    public override void OnExit()
    {

        UnityEngine.Object.Destroy(this._ui_root);

        base.OnExit();
    }
    protected ArrayList _ui_child = new ArrayList();

}



public class UI_xy : ViewUI
{


    public override void Update()
    {
        base.Update();

        var self = BallsMgr.ins.GetSelfBalls();

        txt.text = string.Format(DATA.UI_INFO_XY, self.x.ToString("0"), self.y.ToString("0"));
    }

    public override bool Init()
    {
        base.Init();

        this.txt = GameObject.Find("xy").GetComponent<Text>();

        return true;
    }


    private Text txt;

}
public class UI_rank : ViewUI
{

    public override void Update()
    {
        base.Update();


        Ball ba = BallsMgr.ins.GetSelfBalls();
        ArrayList list = BallsMgr.ins.GetAllBalls();

        StableSort.Sort(ref list, this.cmp);

        string rank = "排名榜:\n";

        int lim = list.Count > 5 ? 5 : list.Count;
        for (int i = 0; i < lim; i++)
        {
            Ball b = (list[i] as Ball);

            rank += b.name + "(" + b.no + ")" + " " + b.value + "\n";

            if (b.no == ba.no)
            {
                BallsMgr.ins.SetSelfRank(i + 1); //自己的排名
            }
        }

        txt.text = rank;

    }

    public override bool Init()
    {
        base.Init();

        this.txt = GameObject.Find("rank").GetComponent<Text>();

        return true;
    }

    McmpBalls cmp = new McmpBalls();
    private Text txt;

}

public class UI_fpsms : ViewUI
{

    public override void Update()
    {
        if (this.isVideoMode) return;
        base.Update();
        ++counter;
        _fps += 1.0f / Time.deltaTime;
        if (counter >= 10)
        {
            txt.text = string.Format(DATA.UI_FPSMS, (_fps / 10.0f).ToString("0"), +PublicData.GetInstance().ms);
            _fps = 0.0f;
            counter = 0;
        }
    }

    public override bool Init()
    {
        base.Init();

        this.txt = GameObject.Find("fps_ms").GetComponent<Text>();
        this.isVideoMode = PublicData.GetInstance().isVideoMode;
        if (this.isVideoMode)
        {
            this.txt.text = DATA.UI_PLAYING_VIDEOMODE;
        }
        return true;
    }


    private Text txt;
    private float _fps = 0.0f;
    private int counter = 0;
    private bool isVideoMode = false;
}


public class UI_info : ViewUI
{

    public override void Update()
    {
        base.Update();

        txt.text = string.Format(DATA.UI_INFO, BallsMgr.ins.GetSelfBalls().value);

    }

    public override bool Init()
    {
        base.Init();

        this.txt = GameObject.Find("info").GetComponent<Text>();

        return true;
    }


    private Text txt;

}


public class UI_frame : ViewUI
{

    public override void Update()
    {
        base.Update();
        int cur = BattleApp.ins.GetCurrentFrame();


        int sec_total = cur / 40;

        int min = sec_total / 60;

        int sec = sec_total - min * 60;
        string s_sec, s_min;
        if (sec < 10)
        {
            s_sec = "0" + sec.ToString();
        }
        else
        {
            s_sec = sec.ToString();
        }

        if (min < 10)
        {
            s_min = "0" + min.ToString();
        }
        else
        {
            s_min = min.ToString();
        }
        txt.text = " 时间: " + s_min + ":" + s_sec + "   " + string.Format(DATA.UI_FRAME, cur);

    }

    public override bool Init()
    {
        base.Init();

        this.txt = GameObject.Find("frame").GetComponent<Text>();

        return true;
    }


    private Text txt;

}


public class UI_death : ViewUI
{

    public override void Update()
    {
        base.Update();
        if (this._enable)
        {
            alive_time.text = string.Format(DATA.UI_DEATH_ALIVE_TIME, BallsMgr.ins.GetSelfBalls().alive_time.ToString("0.00"));
        }

    }



    public override void OnEvent(string type, object userData)
    {
        switch (type)
        {
            case "death":
                {
                    this.ui.SetActive(true);
                    this._enable = true;
                    info.text = userData as string;
                } break;

            case "alive":
                {
                    this.ui.SetActive(false);
                    this._enable = false;
                } break;
        }
    }


    public override bool Init()
    {
        base.Init();

        this.ui = GameObject.Find("beenEat");
        this.info = GameObject.Find("info1").GetComponent<Text>();
        this.alive_time = GameObject.Find("alive").GetComponent<Text>();

        this._enable = false;
        this.ui.SetActive(false);

        EventDispatcher.ins.AddEventListener(this, "death");
        EventDispatcher.ins.AddEventListener(this, "alive");

        return true;
    }


    private GameObject ui;
    private Text info;
    private Text alive_time;
}


public class UI_video : ViewUI
{


    public override void Update()
    {
        base.Update();

    }

    public override bool Init()
    {
        base.Init();

        this.ui = GameObject.Find("Panel_VideoMode");
        this.btn_normal = GameObject.Find("btn_normal").GetComponent<Button>();
        this.btn_2x = GameObject.Find("btn_2x").GetComponent<Button>();
        this.btn_4x = GameObject.Find("btn_4x").GetComponent<Button>();
        this.btn_exit = GameObject.Find("btn_exit").GetComponent<Button>();

        this.btn_normal.onClick.AddListener(delegate()
        {
            this.OnBtn_normal();
        });
        this.btn_2x.onClick.AddListener(delegate()
        {
            this.OnBtn_2x();
        });
        this.btn_4x.onClick.AddListener(delegate()
        {
            this.OnBtn_4x();
        });
        this.btn_exit.onClick.AddListener(delegate()
        {
            this.OnBtn_exit();
        });

        this._enable = PublicData.GetInstance().isVideoMode;
        this.ui.SetActive(_enable);

        return true;
    }

    private void OnBtn_normal()
    {
        BattleApp.ins.SetPlayerModeSpeed(25);
    }

    private void OnBtn_2x()
    {
        BattleApp.ins.SetPlayerModeSpeed(12);
    }


    private void OnBtn_4x()
    {
        BattleApp.ins.SetPlayerModeSpeed(6);
    }



    private void OnBtn_exit()
    {
        BattleApp.ins.SetGameOver();
        //   UnityEngine.SceneManagement.SceneManager.LoadScene("Main");
    }


    private GameObject ui;
    private Button btn_normal;
    private Button btn_2x;
    private Button btn_4x;
    private Button btn_exit;

}





public class UI_dirInput : ViewUI
{
    public override void OnEvent(string type, object userData)
    {

    }

    public override void Update()
    {
        base.Update();
        //  this.ui.SetActive(_enable);


        Ball self = BallsMgr.ins.GetSelfBalls();
        if (!self.alive)
        {//死亡后不显示摇杆
            this.model._enable = false;
        }

        if (this.ui.activeSelf != model._enable)
        {
            this.ui.SetActive(model._enable);
        }

        if (model._enable)
        {
            this.img_arrow.transform.localPosition = new Vector3(model.x_arrow, model.y_arrow, this.img_arrow.transform.localPosition.z);

            this.img_arrow.transform.rotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, model.rotateZ_arrow));

            this.img_center.transform.localPosition = new Vector3(model.x_center, model.y_center, this.img_center.transform.localPosition.z);

            this.img_bg.transform.localPosition = new Vector3(model.x_bg, model.y_bg, this.img_bg.transform.localPosition.z);

        }

    }

    public override bool Init()
    {

        this.ui = PrefabsMgr.Load(DATA.UI_PREFABS_FILE_DIRINPUT);

        DirInput m = ModelMgr.Create<DirInput>() as DirInput;


        this.BindModel(m);


        base.Init();

        //   this.ui = GameObject.Find("DirectionInput");
        this.model = base._model as DirInput;


        this.img_bg = GameObject.Find("bg").GetComponent<Image>();
        this.img_arrow = GameObject.Find("arrow").GetComponent<Image>();
        this.img_center = GameObject.Find("center").GetComponent<Image>();

        // EventDispatcher.ins.AddEventListener(this, "ok");
        return true;
    }

    public override void OnExit()
    {
        PrefabsMgr.Destroy(this.ui);
        base.OnExit();
    }

    private GameObject ui;
    //  private bool _enable_cache = false;
    private Image img_arrow;
    private Image img_bg;
    private Image img_center;
    private DirInput model;
}




public class UI_btn_chat : ViewUI
{

    public override void Update()
    {
        base.Update();

    }

    public override void OnEvent(int type, object userData)
    {
        if (type == Event.ID_CHAT_BTN_close || type == Event.ID_CHAT_BTN_send)
        {
            this.btn.gameObject.SetActive(true);
        }
    }
    public override bool Init()
    {
        base.Init();

        this.btn = GameObject.Find("btn_chat_root").GetComponent<Button>();
        this.btn.onClick.AddListener(delegate()
        {

            this.On_BtnClicked();
        });



        EventDispatcher.ins.AddEventListener(this, Event.ID_CHAT_BTN_send);
        EventDispatcher.ins.AddEventListener(this, Event.ID_CHAT_BTN_close);


        return true;
    }



    private void On_BtnClicked()
    {
        EventDispatcher.ins.PostEvent(Event.ID_BTN_CHAT_ROOT_CLICKED);
        this.btn.gameObject.SetActive(false);
    }
    Button btn;

}






//分裂 UI
public class UI_btn_split : ViewUI
{

    public override void Update()
    {
        base.Update();

    }

    public override void OnEvent(int type, object userData)
    {
        /* if (type == Event.ID_CHAT_BTN_close || type == Event.ID_CHAT_BTN_send)
         {
             this.btn.gameObject.SetActive(true);
         }*/
    }
    public override bool Init()
    {
        base.Init();

        this.btn = GameObject.Find("btn_split_root").GetComponent<Button>();
        this.btn.onClick.AddListener(delegate()
        {

            this.On_BtnClicked();
        });




        return true;
    }



    private void On_BtnClicked()
    {

        //   this.btn.gameObject.SetActive(false);
       PublicData.GetInstance().IS_SPLIT_PRESS = true;
     //   Debug.Log("split    111111111");

    }
    Button btn;

}



//孢子
public class UI_btn_child : ViewUI
{

    public override void Update()
    {
        base.Update();

    }

    public override void OnEvent(int type, object userData)
    {
        /* if (type == Event.ID_CHAT_BTN_close || type == Event.ID_CHAT_BTN_send)
         {
             this.btn.gameObject.SetActive(true);
         }*/
    }
    public override bool Init()
    {
        base.Init();

        this.btn = GameObject.Find("btn_child_root").GetComponent<Button>();
        this.btn.onClick.AddListener(delegate()
        {

            this.On_BtnClicked();
        });




        return true;
    }



    private void On_BtnClicked()
    {

        //   this.btn.gameObject.SetActive(false);
        PublicData.GetInstance().IS_CHILD_PRESS = true;
     //   Debug.Log("launch a child     111111111");

    }
    Button btn;

}

